/* eslint-disable prefer-destructuring */
import {
  getOrderTypeInfo,
  queryOriginalFactoryList,
  updateBatch,
  saveOrderType
} from '@api/system_config_v1.js';

export default {
  name: 'orderTypeConfigurationForm',
  props: {
    supplierCode: String
  },
  data() {
    return {
      params: {
        originalFactoryCode: ''
      },
      originalFactoryList: [],
      originalFactoryMap: {},
      checkList: [],
      selectList: [],
      orderTypeList: [],
      loading: false,
      orderTypeInfo: []
    };
  },
  created() {
    // this.initGlobalBtn()
    this.setCheckBoxList();
  },

  methods: {
    getInfo() {
      // this.initGlobalBtn()
      this.$axios
        .$post(getOrderTypeInfo, {
          supplierCode: this.supplierCode
        })
        .then(res => {
          res.payload.content.forEach(item => {
            const type = this.orderTypeList.filter(
              // eslint-disable-next-line no-shadow
              type => type.text === item.orderTypeName
            );
            if (type.length) {
              type[0].label += `,${item.orderTypeId}`;
              this.checkList.push(type[0].label);
            }
          });
          this.setOriginList([
            {
              originalFactoryCode: this.supplierCode,
              originalFactoryName: res.payload.content[0].supplierName
            }
          ]);
          this.params.originalFactoryCode = this.supplierCode;
        });
    },
    setCheckBoxList() {
      this.orderTypeList = [
        {
          label: 'WHOLESALE_PROM,24,促销订单',
          text: '促销订单'
        },
        {
          label: 'WHOLESALE_REBATE,26,返利订单',
          text: '返利订单'
        },
        {
          label: 'WHOLESALE_CREDIT,28,积分订单',
          text: '积分订单'
        }
      ];
    },
    initGlobalBtn() {
      const btnObj = {
        type: 'btn',
        arr: []
      };
      const submitBtn = {
        type: 'primary',
        val: this.supplierCode ? '修改' : '提交',
        handle: this.onSubmit
      };
      btnObj.arr.push(submitBtn);
      this.$store.commit('changeLayoutHandle', btnObj);
    },
    clearForm() {
      this.params.originalFactoryCode = '';
      this.checkList = [];
    },
    // eslint-disable-next-line consistent-return
    onSubmit() {
      let sendData = [];
      let next = true;
      let errMsg = '';
      if (!this.params.originalFactoryCode) {
        next = false;
        errMsg = '请选择原厂';
      }
      if (!this.checkList.length) {
        if (next) {
          next = false;
          errMsg = '请选择订单类型';
        }
      }
      if (!next) return this.$message.warning(errMsg);
      this.checkList.forEach(item => {
        const typeData = item.split(',');
        const obj = {
          ddbm: typeData[0],
          orderTypeCode: typeData[1],
          orderTypeName: typeData[2],
          supplierCode: this.params.originalFactoryCode,
          supplierName: this.originalFactoryMap[this.params.originalFactoryCode]
        };
        // eslint-disable-next-line no-unused-expressions
        this.supplierCode && (obj.orderTypeId = typeData[3]);
        sendData.push(obj);
      });
      let ex = saveOrderType;
      let msg = '添加成功';
      if (this.supplierCode) {
        ex = updateBatch;
        msg = '修改成功';
      }
      sendData = {
        supplierCode: this.supplierCode,
        dataList: sendData
      };
      this.$axios.$post(ex, sendData).then(() => {
        this.$message.success(msg);
        // this.$router.go(-1)
        this.$emit('on-save-success');
      });
    },
    // 原厂下拉
    queryOriginalFactoryList(val) {
      if (val !== '') {
        this.loading = true;
        const query = {
          originalFactoryName: val
        };
        this.$axios.$post(queryOriginalFactoryList, query).then(res => {
          this.setOriginList(res.payload);
          this.loading = false;
        });
      }
    },
    setOriginList(data) {
      this.originalFactoryList = data;
      data.forEach(item => {
        this.originalFactoryMap[item.originalFactoryCode] =
          item.originalFactoryName;
      });
    }
  }
};
